package com.ts.host;

import java.io.File;
import java.util.Map;
import java.util.Random;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.rio.core.L;
import com.rio.core.S;
import com.rio.core.U;
import com.rio.helper.BitmapHelper;
import com.rio.utils.TabHelper;
import com.rio.utils.TabHelper.OnTabChangeListener;
import com.rio.utils.URLRequest;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.LayoutAnimationController;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Adapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class T {
	
	/**
	 * 获取倒计时的随机数 
	 * 
	 * @return
	 * @delyed 秒
	 */
	public static long getDownCountRandom(int delyed) {
		Random rand = new Random();
		return (rand.nextInt(delyed) + delyed) * 1000;

	}



	public static void setImage(View view, int id, File file) {
		ImageView v = U.findViewById(view, id);
		if (U.notNull(v)) {
			BitmapHelper bh = BitmapHelper.open(file);
			if (U.notNull(bh) && !bh.isEmpty()) {
				v.setImageBitmap(bh.get());
			}
		}
	}

	public static void setBackground(View view, int id, int res) {
		View v = U.findViewById(view, id);
		if (U.notNull(v)) {
			v.setBackgroundResource(res);
		}
	}

	public static void setTextColor(View view, int id, int color) {
		TextView v = U.findViewById(view, id);
		if (U.notNull(v)) {
			v.setTextColor(APP.getColor(color));
		}
	}

	public static void setText(View view, int id, String text) {
		TextView v = U.findViewById(view, id);
		if (U.notNull(v)) {
			v.setText(text);
		}
	}

	public static void setText(View view, int id, String text,String base ) {
		TextView v = U.findViewById(view, id);
		if (U.notNull(v)) {
			if(U.notNull(text)){
				v.setText(text);
			}else{
				v.setText(base);
			}			
		}
	}
	
//	public static void setImage(View view, int id, String url) {
//		if (U.notNull(url)) {
//			ImageView v = U.findViewById(view, id);
//			if (U.notNull(v)) {
//				ImageLoader.getInstance().displayImage(url, v,
//						APP.getImageOptions(), null, null);
//			}
//		}
//	}
//
//	public static void setImage(ImageView v, String url) {
//
//		if (U.notNull(v) && U.notNull(url)) {
//			ImageLoader.getInstance().displayImage(url, v,
//					APP.getImageOptions(), null, null);
//		}
//
//	}

	public static void setImage(View view, int id, Bitmap bitmap) {
		if (U.notNull(bitmap) && !bitmap.isRecycled()) {
			ImageView v = U.findViewById(view, id);
			if (U.notNull(v)) {
				v.setImageBitmap(bitmap);
			}
		}
	}

	public static void setImage(View view, int id, int drawer) {

		ImageView v = U.findViewById(view, id);
		if (U.notNull(v)) {
			v.setImageResource(drawer);
		}
	}
	public static void setLayoutAnimation(ListView view){
		if(U.notNull(view)){
			AnimationSet set = new AnimationSet(false);
			Animation animation = new AlphaAnimation(0,1);   //AlphaAnimation 控制渐变透明的动画效果
			animation.setDuration(500);     //动画时间毫秒数
			set.addAnimation(animation);    //加入动画集合

			animation = new TranslateAnimation(1, 13, 10, 50);  //ScaleAnimation 控制尺寸伸缩的动画效果
			animation.setDuration(300);
			set.addAnimation(animation);

			animation = new RotateAnimation(30,10);    //TranslateAnimation  控制画面平移的动画效果
			animation.setDuration(300);
			set.addAnimation(animation);

			animation = new ScaleAnimation(5,0,2,0);    //RotateAnimation  控制画面角度变化的动画效果
			animation.setDuration(300);
			set.addAnimation(animation);

			LayoutAnimationController controller = new LayoutAnimationController(set, 1);
			view.setLayoutAnimation(controller);
		}
	}
	
	public static void setLayoutAnimation(View view, int id){
		PullToRefreshListView v = U.findViewById(view, id);
		if (U.notNull(v)) {
			setLayoutAnimation(v.getRefreshableView());
		}
	}

	public static void setListAdapter(View view, int id, ListAdapter adapter) {
		if (U.notNull(adapter)) {
			PullToRefreshListView v = U.findViewById(view, id);
			if (U.notNull(v)) {
				v.setAdapter(adapter);
				
			}
		}
	}

	public static void setEmptyView(View view, int id) {
			PullToRefreshListView v = U.findViewById(view, id);
			if (U.notNull(v)) {
				View empty = APP.inflate(R.layout.empty);
				v.setEmptyView(empty);				
			}
		
	}
	
	public static void setEmptyView(ListView v, int id) {
		if (U.notNull(v)) {
			View empty = APP.inflate(R.layout.empty);
			v.setEmptyView(empty);				
		}
	}
	
	
	public static void setLoadingText(View view, int id) {
		PullToRefreshListView v = U.findViewById(view, id);
		if (U.notNull(v)) {
			setLoadingText(v.getRefreshableView());
		}		
	}
	
	public static void setLoadingText(ListView v) {
		if (U.notNull(v)) {
			View empty = v.getEmptyView();
			if(U.notNull(empty))T.setText(empty, R.id.text, "正在加载中...");
		}
	}
	
	public static void setEmptyText(View view, int id,String msg) {
		PullToRefreshListView v = U.findViewById(view, id);
		if (U.notNull(v)) {
			setEmptygText(v.getRefreshableView(),msg);
		}		
	}
	
	public static void setEmptygText(ListView v,String msg) {
		if (U.notNull(v)) {
			View empty = v.getEmptyView();
			if(U.notNull(empty))T.setText(empty, R.id.text, msg);
		}
	}
	
	public static void setRefreshComplete(View view, int id) {
		PullToRefreshListView v = U.findViewById(view, id);
		if (U.notNull(v)) {
			v.onRefreshComplete();
		}
	}


	




	public static void setOnClickListener(View view, OnClickListener l, int id) {
		View v = U.findViewById(view, id);
		if (U.notNull(v)) {
			v.setOnClickListener(l);
		}
	}

	public static void setOnClickListener(View view, OnClickListener l,
			int... ids) {
		if (U.notNull(ids)) {
			for (int id : ids) {
				setOnClickListener(view, l, id);
			}
		}
	}

	public static void setEnabled(View view, boolean isEnabled, int... ids) {
		if (U.notNull(ids)) {
			for (int id : ids) {
				View v = U.findViewById(view, id);
				v.setEnabled(isEnabled);
			}
		}
	}


	public static TabHelper<View> bindTab(View view, int check,
			OnTabChangeListener<View> listener, int... ids) {
		TabHelper<View> tab = new TabHelper<View>(view, ids, listener);
		tab.setCheckedItem(check, false);
		return tab;
	}

	public static void gone(View view, int id) {
		View v = U.findViewById(view, id);
		if (U.notNull(v)) {
			v.setVisibility(View.GONE);
		}
	}

	public static void gone(View view, int... ids) {
		if (U.notNull(ids)) {
			for (int id : ids) {
				gone(view, id);
			}
		}
	}

	public static void hide(View view, int id) {
		View v = U.findViewById(view, id);
		if (U.notNull(v)) {
			v.setVisibility(View.INVISIBLE);
		}
	}

	public static void hide(View view, int... ids) {
		if (U.notNull(ids)) {
			for (int id : ids) {
				hide(view, id);
			}
		}
	}

	public static void show(View view, int id) {
		View v = U.findViewById(view, id);
		if (U.notNull(v)) {
			v.setVisibility(View.VISIBLE);
		}
	}

	public static void show(View view, int... ids) {
		if (U.notNull(ids)) {
			for (int id : ids) {
				show(view, id);
			}
		}
	}

	public static void refreshComplete(View view, int id) {
		PullToRefreshBase v = U.findViewById(view, id);
		if (U.notNull(v)) {
			v.onRefreshComplete();
		}
	}
	

	public static void pullComplete(View view,  int id,boolean isShowToast) {
		if(isShowToast){
			TToast.show("已加载完毕");
		}
		PullToRefreshBase v = U.findViewById(view, id);
		if (U.notNull(v)) {
			v.onRefreshComplete();
			v.setMode(Mode.PULL_FROM_START);

		}
	}

	public static void pullComplete(View view, int id) {
		TToast.show("已加载完毕");
		PullToRefreshBase v = U.findViewById(view, id);
		if (U.notNull(v)) {
			v.onRefreshComplete();
			v.setMode(Mode.PULL_FROM_START);

		}
	}

	public static void resetRefresh(View view, int id) {
		PullToRefreshBase v = U.findViewById(view, id);
		if (U.notNull(v)) {
			v.setMode(Mode.BOTH);
		}
	}

	public static void setOnRefreshListener(View view,
			TOnRefreshListener listener, int id) {
		PullToRefreshListView v = U.findViewById(view, id);
		if (U.notNull(v)) {
			v.getRefreshableView().setSelector(R.color.transparent);
			v.setOnRefreshListener(listener);
			v.setOnItemClickListener(listener);
		}
	}

	public static void showToast(Context context, String msg) {
		TToast.show(msg);
	}




}
